﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Company.Shell.Controls
{
    /// <summary>
    /// Heater.xaml 的交互逻辑
    /// </summary>
    public partial class Heater : UserControl
    {
        private Brush closeBrush;
        private Brush openBrush = new SolidColorBrush(Color.FromRgb(75, 169, 69));//绿色
        public Heater()
        {
            InitializeComponent();
            closeBrush = _border.Background;
        }

        public bool IsOpen
        {
            get { return (bool)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        }

        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register("IsOpen", typeof(bool), typeof(Heater), new PropertyMetadata(false, new PropertyChangedCallback(IsOpenChangedCallback)));

        private static void IsOpenChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Heater)d;
            if (control == null)
                return;
            if (e.NewValue is bool isopen) 
            {
                if (isopen)
                {
                    control._border.Background = control.openBrush;
                    control._switch.Text = "ON";
                    control._path.Stroke.BeginAnimation(SolidColorBrush.ColorProperty, control.colorAnimation);
                }
                else 
                {
                    control._border.Background = control.closeBrush;
                    control._switch.Text = "OFF";
                    control._path.Stroke.BeginAnimation(SolidColorBrush.ColorProperty, null);

                }
            }
        }



        /// <summary>
        /// 温度值
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Heater), new PropertyMetadata("00.0℃",new PropertyChangedCallback(TextChangedCallback)));

        private static void TextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Heater)d;
            if (control == null)
                return;
            if(e.NewValue != null)
            {
                control._value.Text = (string)e.NewValue + "℃";
            }
        }

        //创建颜色动画
        private ColorAnimation colorAnimation = new ColorAnimation()
        {
            From = Color.FromRgb(219, 219, 219),
            To = Color.FromRgb(255, 0, 0),
            Duration = TimeSpan.FromSeconds(2),
            AutoReverse = true,
            RepeatBehavior = RepeatBehavior.Forever,
        };
    }
}
